const nodemailer = require('nodemailer');

module.exports = {
    /**
     * 发送邮件
     * @param options
     *  {
     *      host:邮箱服务器,
     *      port:邮箱服务器端口,
     *      service:"简写服务器"//"126","163","1und1","AOL","DebugMail","DynectEmail","FastMail","GandiMail","Gmail","Godaddy","GodaddyAsia","GodaddyEurope","hot.ee","Hotmail","iCloud","mail.ee","Mail.ru","Maildev","Mailgun","Mailjet","Mailosaur","Mandrill","Naver","OpenMailBox","Outlook365","Postmark","QQ","QQex","SendCloud","SendGrid","SendinBlue","SendPulse","SES","SES-US-EAST-1","SES-US-WEST-2","SES-EU-WEST-1","Sparkpost","Yahoo","Yandex","Zoho","qiye.aliyun"
     *      user:邮箱账号，
     *      pass:邮箱密码，
     *      to:接收者
     *      title:邮箱主题
     *      content:邮箱内容
     *  }
     * @returns {Promise}
     */



    send: async options => {
        let option={};
        if(options.service){
            option.service=options.service;
        }else{
            option.host= options.host;
            option.port= options.port ? options.port : 465;
        }
        option.secureConnection=true;
        option.auth={
            user: options.user,//邮箱账号
            pass: options.pass//邮箱密码
        }

        let transporter = nodemailer.createTransport(option);
        let mailOptions = {
            from: options.user, //sender address
            to: options.to, // list of receivers
            subject: options.title, // Subject line
            // text: 'Nodejs之邮件发送', // plaintext body
            html: options.content
        };
        return new Promise(res => {
            transporter.sendMail(mailOptions, function (error, info) {
                if (!error) {
                    res(true);
                } else {
                    console.log(error);
                    res(false);
                }
            });
        })
    }
}




